import SelectAll from '@/components/SelectAll'
import DatePicker from '@/components/Choose/DatePicker'
import MoneyRange from '@/components/Choose/MoneyRange'
import Channel from '@/components/Choose/Channel'
import ChnPaySel from '@/components/Choose/ChnPaySel'
import UserList from '@/components/Choose/UserList'
import InterestList from '@/components/Choose/InterestList'
import TimeInterval from '@/components/Choose/TimeInterval'
import UserType from '@/components/Choose/UserType'
import Role from '@/components/Choose/Role'
import AnalyseTable from '@/components/AnalyseTable'
import AnalyseOnlineTable from '@/components/AnalyseTable/AnalyseOnlineTable'
import { fixed3 } from '@/utils'
import { sendForm } from '@/utils/'
import {useVcode} from '@/utils/Vcode'

export default {
  data() {
    return {
      resData: {},
      key_mapper: [],
      page: 1,
      size: 40,
      total: 0,
      sendData: {},
      page_id: this.$route.meta.perm && this.$route.meta.perm.split('|')[1],
      tableData: [],
      excelURl: '',
      excelLoad: false,
      level:1
    }
  },
  components: {
    SelectAll,
    DatePicker,
    MoneyRange,
    Channel,
    ChnPaySel,
    UserList,
    InterestList,
    TimeInterval,
    AnalyseTable,
    AnalyseOnlineTable,
    Role,
    UserType
  },
  activated() {
    this.getData()
  },
  deactivated() {
      this.listLoading = false
  },
  methods: {
    getExcel(data = this.queryFrom) {
      let send = Object.assign({}, data, { to_export_file: 1 })
      send = sendForm(send)
      this.excelLoad = true
      return this.API(send).then(res => {
        this.excelURl = res.data.url
        this.excelLoad = false
        console.log(process.env.BASE_API + this.excelURl)
      })
    },
    getData(data = this.queryFrom, API = this.API, key_mapper = 'key_mapper', tableData = 'tableData') {
      this.$loading({ lock: true, text: 'Loading', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.15)' })
      let send = Object.assign({}, data, { page: this.page, size: this.size, page_id: this.page_id })
      send = sendForm(send)
      // this.getExcel(send)
      API(send).then(res => {
        this.sendData = JSON.parse(JSON.stringify(send))
        this.resData = res.data
        this[tableData] = res.data.list.map(item => {
          const data = {}
          for (const item1 in item) {
            if (item[item1]) {
              data[item1] = item[item1].sum || item[item1]
            } else if (item[item1] === '') {
              data[item1] = ' '
            } else {
              data[item1] = null
            }
          }
          return data
        })
        if (res.data.key_mapper) {
          this[key_mapper] = res.data.key_mapper
        }
        this.total = res.data.total_count
      }, err => {
        this.$message({ type: 'error', message: err.response.data.msg })
      })
    },
    currentChange(val) {
      this.sendData.page = val
      this.getData(this.sendData)
    },
    sendQuery() {
      this.page = 1
      this.level = 1
      if(this.queryFrom && this.queryFrom.game_type && this.queryFrom.game_type.length>0){
        this.level = 2
      }
      if(this.queryFrom && this.queryFrom.game_id_list && this.queryFrom.game_id_list.length>0){
        this.level = 3
      }
      this.getData()
    },
    sortChange(value) {
      if (value.sort_by === 'undefinednull') {
        return
      }
      const data = Object.assign(this.sendData, value)
      this.getData(data)
    },
    handleSizeChange(val) {
      this.size = val
      this.sendData.size = val
      this.getData(this.sendData)
    },
     downExcel() {
       useVcode(this, () => {
         this.getExcel_()
       })
    },
  async getExcel_(){
    await this.getExcel(this.sendData)
    this.excelURl && window.open(process.env.BASE_API + this.excelURl)
    },
    formatter(row, column, item) {
      return fixed3(item)
    }
  }
}
